/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SettingsDialog.java
 *
 * Created on 19-giu-2011, 14.56.47
 */
package settings.GUI.components;

import com.ardor3d.renderer.lwjgl.LwjglContextCapabilities;
import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.GraphicsEnvironment;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import org.lwjgl.opengl.GLContext;
import settings.GameSettings;
import utils.Display.DisplayModeSorter;

/**
 *
 * @author Fra
 */
public class SettingsDialog extends JFrame {

    /** A return status code - returned if Cancel button has been pressed */
    public static final int RET_CANCEL = 0;
    /** A return status code - returned if OK button has been pressed */
    public static final int RET_OK = 1;
    private Object[] resolutions;
    private Object[] antialiases;
    private GameSettings settings;
    
    public SettingsDialog(GameSettings gameSettings) {
        this();
        settings = gameSettings;
        updateGUIFromSettings();
    }

    public String getScreenResolution() {
        return (String) jComboBox1.getSelectedItem();
    }

    public boolean isFullScreenSelected(){
        return jCheckBox1.isSelected();
    }

    /** Creates new form SettingsDialog */
    public SettingsDialog() {
        super();
        getContextInformation();
        initComponents();
    }

    /** @return the return status of this dialog - one of RET_OK or RET_CANCEL */
    public int getReturnStatus() {
        return returnStatus;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jComboBox5 = new javax.swing.JComboBox();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel4 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jCheckBox1 = new javax.swing.JCheckBox();
        jLabel3 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        jComboBox5.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("JSpace3D Settings");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jPanel1.setLayout(new java.awt.GridBagLayout());

        jScrollPane1.setBorder(null);

        jPanel4.setLayout(new java.awt.GridBagLayout());

        jLabel1.setFont(new java.awt.Font("sansserif", 1, 12));
        jLabel1.setText("Resolution:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(12, 10, 0, 120);
        jPanel4.add(jLabel1, gridBagConstraints);

        jComboBox1.setModel(new DefaultComboBoxModel(resolutions));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 10);
        jPanel4.add(jComboBox1, gridBagConstraints);

        jLabel2.setFont(new java.awt.Font("sansserif", 1, 12));
        jLabel2.setText("Antialias:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(12, 10, 0, 0);
        jPanel4.add(jLabel2, gridBagConstraints);

        jComboBox2.setModel(new DefaultComboBoxModel(antialiases));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 10);
        jPanel4.add(jComboBox2, gridBagConstraints);

        jCheckBox1.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(11, 0, 0, 10);
        jPanel4.add(jCheckBox1, gridBagConstraints);

        jLabel3.setFont(new java.awt.Font("sansserif", 1, 12)); // NOI18N
        jLabel3.setText("Force GUI on restart");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(12, 10, 0, 0);
        jPanel4.add(jLabel3, gridBagConstraints);

        jScrollPane1.setViewportView(jPanel4);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        jPanel1.add(jScrollPane1, gridBagConstraints);

        jTabbedPane1.addTab("Graphics", jPanel1);

        jPanel2.setLayout(new java.awt.GridBagLayout());
        jTabbedPane1.addTab("Sound", jPanel2);

        jPanel3.setLayout(new java.awt.GridBagLayout());
        jTabbedPane1.addTab("Network", jPanel3);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(8, 8, 8, 8);
        getContentPane().add(jTabbedPane1, gridBagConstraints);

        jButton1.setFont(new java.awt.Font("Comic Sans MS", 0, 36));
        jButton1.setText("PLEI");
        jButton1.setMaximumSize(new java.awt.Dimension(200, 150));
        jButton1.setMinimumSize(new java.awt.Dimension(100, 80));
        jButton1.setPreferredSize(new java.awt.Dimension(100, 80));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(8, 8, 8, 8);
        getContentPane().add(jButton1, gridBagConstraints);

        jMenu1.setText("File");

        jMenuItem1.setText("Exit");
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        doClose(RET_CANCEL);
    }//GEN-LAST:event_closeDialog

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_jComboBox1ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        updateSettingsFromGUI();
        doClose(RET_OK);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void doClose(int retStatus) {
        returnStatus = retStatus;
        setVisible(false);
        dispose();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                setDefaultLookAndFeelDecorated(true);
                SettingsDialog dialog = new SettingsDialog();
                dialog.setLocationRelativeTo(null);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.pack();
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JComboBox jComboBox5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables
    private int returnStatus = RET_CANCEL;

    private void getContextInformation() {
        //Get info on current context
        DisplayMode modes[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayModes();
        Arrays.sort(modes, new DisplayModeSorter());

        final ArrayList<String> _resolutions = new ArrayList<String>(modes.length);

        for (int i = 0; i < modes.length; i++) {
            String string = modes[i].getWidth() + " x " + modes[i].getHeight();
            if (!_resolutions.contains(string)) {
                _resolutions.add(string);
            }

        }
        resolutions = _resolutions.toArray();

        antialiases = new Object[]{ "none", "2x" , "4x", "8x", "16x" };
    }

    private void updateGUIFromSettings() {
        setResolution();
        setAntialias();
        setGUIBoolean();
    }

    private void updateSettingsFromGUI(){
        settings.setResolution(resolutionFromString((String)jComboBox1.getSelectedItem()));
        settings.setAntialias(antialiasFromString((String)jComboBox2.getSelectedItem()));
        settings.setForceGUI(jCheckBox1.isSelected());
    }

    private void setResolution() {
        for(int i = 0; i<jComboBox1.getItemCount(); i++){            
            Dimension GUIDimension = resolutionFromString((String) jComboBox1.getItemAt(i));
            if(settings.getResolution().equals(GUIDimension)){
                jComboBox1.setSelectedIndex(i);
                break;
            }
        }
    }

    private void setAntialias() {
        for(int i = 0; i<jComboBox2.getItemCount(); i++){
            int antialias = antialiasFromString((String)jComboBox2.getItemAt(i));
            if(antialias == settings.getAntialias()){
                jComboBox2.setSelectedIndex(i);
                break;
            }
        }
    }

    private void setGUIBoolean() {
        if(settings.isForceGUI())
            jCheckBox1.setSelected(true);
        else
            jCheckBox1.setSelected(false);
    }

    private Dimension resolutionFromString(String resolutionStr) {
            String widthStr = resolutionStr.split(" x ")[0];
            String heightStr = resolutionStr.split(" x ")[1];
            int width = Integer.parseInt(widthStr);
            int height = Integer.parseInt(heightStr);
            Dimension out = new Dimension(width,height);
            return out;
    }

    private int antialiasFromString(String antialiasStr) {
        if(antialiasStr.equals("none"))
            return 0;
        antialiasStr = antialiasStr.split("x")[0];
        return Integer.parseInt(antialiasStr);
    }
}
